package net.dlz.example.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.dlz.example.domain.Policy;
import net.dlz.example.domain.PolicyExample;
import net.dlz.example.domain.SearchPolicyResponse;
import net.dlz.example.mapper.PolicyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PolicyServiceImpl implements PolicyService{

    @Autowired
    private PolicyMapper policyMapper;

    @Override
    public List<Policy> listPolicy() {
            return policyMapper.selectByExample(new PolicyExample());
    }

    @Override
    public SearchPolicyResponse findAllPolicy(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Policy> policys = policyMapper.selectByExample(new PolicyExample());
        return new SearchPolicyResponse(((Page)policys).getTotal(),policys);
    }

    @Override
    public SearchPolicyResponse findPolicyLinkTopic(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PolicyExample example = new PolicyExample();
        example.createCriteria().andPolicyTopicLike("%"+ keywords + "%");
        List<Policy> policys = policyMapper.selectByLinkTopic(keywords);
        return new SearchPolicyResponse(((Page)policys).getTotal(),policys);
    }

    @Override
    public int savePolicy(Policy policy) {
        System.out.println("Service:"+policy.getPolicyTopic());
        return policyMapper.insert(policy);
    }

    @Override
    public int deletePolicy(String id) {
        return policyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Policy findPolicyByPrimaryKey(String id) {
        return policyMapper.selectByPrimaryKey(id);
    }
}
